<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Must start with 'MY_', configuration at config.php, $config['subclass_prefix'] = 'MY_'
 */
class MY_Controller extends CI_Controller{

	/**
	 * controller's data
	 * @var mixed
	 */
	public $viewBag;

    /**
	 * construct
     */
    public function __construct(){
        parent::__construct();
        $this->load->helper(array('form','url'));
		$this->viewBag = array();
    }

	public function setViewBag($data){
		$this->viewBag = $data;
	}

	public function setViewBagKey($key, $data){
		$this->viewBag[$key] = $data;
	}

	public function loadHead($title, $description){
		$this->viewBag["head"] = array( "title" => $title, "description" => $description );
	}
	public function loadog($type,$url,$image){
		$this->viewBag["og"] = array(
			"type" => $type,  //article book movie
			"url" => $url,
			"image" =>$image
		);
	}

	public function getViewBag(){
		return $this->viewBag;
	}

	public function getViewBagKey($key){
		return $this->viewBag[$key];
	}

	/**
	 * load view
	 * @param mixed $data
	 */
	public function loadView( $data, $controller, $view ){
		$this->load->view('layouts/header.html', $data);
    	$this->load->view("$controller/$view");
    	$this->load->view('layouts/footer.html');
	}

    public function loadUserView( $data, $controller, $view ){
		$this->load->view('layouts/userHeader.html', $data);
    	$this->load->view("$controller/$view");
    	$this->load->view('layouts/userFooter.html');
	}

	public function loadAdminView($data, $view){
		$this->load->view('admin/headerAdmin.html', $data);
		$this->load->view("admin/$view.html");
		$this->load->view('admin/footerAdmin.html');
	}

	/**
	 * load view
	 * @param mixed $data
	 */
	public function loadFullView( $data, $fullview ){
		$this->load->view('layouts/header.html', $data);
    	$this->load->view("$fullview");
    	$this->load->view('layouts/footer.html');
	}

	/**
	 * load array view
	 */
	public function loadArrayView( $data, $arrayview ){
		$this->load->view('layouts/header.html', $data);
    	foreach($arrayview as $value){
			$this->load->view("$value");
		}
    	$this->load->view('layouts/footer.html');
	}

	/**
	 * check is login
	 * @return boolean
	 */
	public function islogin(){
		if(empty($_SESSION["currentUser"]) == false){
			return true;
		}
		else{
			return false;
		}
	}

	/**
	 * get login user id, if haven't login, return 0
	 */
	public function getLoginUserId(){
		if(empty($_SESSION["currentUser"]) == false){
			return $_SESSION["currentUser"]["user_id"];
		}
		else{
			return 0;
		}
	}

	 /**
	 * get login username, if haven't login, return ''
	 */
	public function getLoginUsername(){
		if(empty($_SESSION["currentUser"]) == false){
			return $_SESSION["currentUser"]["username"];
		}
		else{
			return '';
		}
	}

	/**
	 * get login user_headimg, if haven't login, return ''
	 */
	public function getLoginUserHeadimg(){
		if(empty($_SESSION["currentUser"]) == false){
			return $_SESSION["currentUser"]["user_headimg"];
		}
		else{
			return '';
		}
	}

	/**
	 * get login display_name, if haven't login, return ''
	 */
	public function getLoginUserDisplayname(){
		if(empty($_SESSION["currentUser"]) == false){
			return $_SESSION["currentUser"]["display_name"];
		}
		else{
			return '';
		}
	}

    public function getLoginUserPhone(){
		if(empty($_SESSION["currentUser"]) == false){
			return $_SESSION["currentUser"]["phone"];
		}
		else{
			return '';
		}
	}

    public function getLoginUserToken(){
		if(empty($_SESSION["currentUser"]) == false){
			return $_SESSION["currentUser"]["token"];
		}
		else{
			return '';
		}
	}

    public function getLoginParentID(){
		if(empty($_SESSION["currentUser"]) == false){
			return intval($_SESSION["currentUser"]["parent_id"]);
		}
		else{
			return 0;
		}
	}

    public function getLoginGroupID(){
		if(empty($_SESSION["currentUser"]) == false){
			return intval($_SESSION["currentUser"]["group_id"]);
		}
		else{
			return 0;
		}
	}

    public function getLoginUserRegisterTime(){
        if(empty($_SESSION["currentUser"]) == false){
            return strtotime($_SESSION["currentUser"]["add_time"]);
        }
        else{
            return 0;
        }
    }

	/**
	 * get login user's info
	 */
	public function getLoginUserInfo(){
		if( $this->islogin() == false ){
			return array( 'id' => 0, 'user_name' => '', 'display_name' => '', 'user_headimg' => '' );
		}
		else{
			return array(
				'id' => intval($_SESSION['currentUser']['user_id']),
				'user_name' => $_SESSION['currentUser']['username'],
				'display_name' => $_SESSION['currentUser']['display_name'],
				'user_headimg' => $_SESSION['currentUser']['user_headimg'],
                'add_time' => $_SESSION['currentUser']['add_time']
			);
		}
	}

	public function isAdmin(){
		if( empty($_SESSION["currentUser"]) == true ){
            return false;
        }
		if( $_SESSION["currentUser"]["role"] != "admin" ){
			return false;
		}
		return true;
	}
}